﻿using System;

namespace DotNetStar.OpenSDK
{
    /// <summary>
    /// 表示一个单例模式。
    /// </summary>
    /// <typeparam name="TObject">需要单例模式的类型。</typeparam>
    public sealed class Singleton<TObject> where TObject : class
    {
        static TObject _instance;
        static object _locker = new object();

        /// <summary>
        /// 创建指定类型的单例实例。
        /// </summary>
        /// <param name="args">指定类型构造函数的参数，个数、数据类型、顺序必须符合已有的构造函数。</param>
        /// <returns>指定对象的单例实例。</returns>
        public static TObject CreateInstance(params object[] args)
        {
            if (_instance == null)
            {
                lock (_locker)
                {
                    if (_instance == null)
                    {
                        _instance = (TObject)Activator.CreateInstance(typeof(TObject), args);
                    }
                }
            }
            return _instance;
        }
    }
}
